package cn.com.cfae.iras.doc.analyze.parse.extractor;

import cn.com.cfae.iras.doc.analyze.parse.model.*;
import cn.com.cfae.iras.doc.analyze.parse.tokenizer.ZCFZTokenizer;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;


/**
 * 获取M2涉及财务基础科目数据
 */
public class M2Extractor extends MTableExtractor {
    private static final Logger logger = LoggerFactory.getLogger(M2Extractor.class);
    private ZCFZTokenizer zcfzTokenizer = ZCFZTokenizer.getInstance();

    @Override
    public void extract() {
        try {
            extractM2();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }


    public M2Extractor() {
        super();
    }

    private void extractM2() {
        long t1 = System.currentTimeMillis();
        logger.info("开始抽取{}章业务模型数据......", getChapterModel().getChapterId());
        try {
            parseChapterSection();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        try {
            extractM2Zcfz();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        try {
            extractM2Xjll();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        try {
            extractM2Lr();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        long t2 = System.currentTimeMillis();
        logger.info("抽取{}章业务模型数据完成，耗时：{}毫秒。", getChapterModel().getChapterId(), (t2 - t1));
    }

    /**
     * 资产负债
     */
    private void extractM2Zcfz() {
        long t1 = System.currentTimeMillis();
        logger.info("开始抽取{}章资产负债表会计科目数据......", getChapterModel().getChapterId());
        ExtractBizModel extractBizModel = getChapterModel().getExtractBizModelMap().get("ZCFZ");
        try {
            //TODO 后续改成动态注入
            zcfzTokenizer.loadCoreSynonyms(extractBizModel);

            List<SectionModel> sectionModelList = getChapterModel().getSectionModelList();
            if (CollectionUtils.isEmpty(sectionModelList)) {
                logger.error("抽取{}章财务报表会计科目分析，会计科目分析列表为空，请确认！！！", getChapterModel().getChapterId());
                return;
            }
            try {
                for (int i = 0; i < sectionModelList.size(); i++) {
                    SectionModel sectionModel = sectionModelList.get(i);
                    List<SectionModel> childSectionModelList = sectionModel.getChildSectionModelList();
                    if (CollectionUtils.isEmpty(childSectionModelList)) {
                        continue;
                    }
                    for (int j = 0; j < childSectionModelList.size(); j++) {
                        SectionModel childSectionModel = childSectionModelList.get(j);
                        String content = childSectionModel.getContent();
                        if (isEmpty(content)) {
                            continue;
                        }
                        logger.info("抽取{}章业务模型数据，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), extractBizModel.getModelName(), childSectionModel.getTitle());
                        zcfzTokenizer.splitWords5(extractBizModel, childSectionModel);
                    }
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            zcfzTokenizer.unloadCoreSynonyms(extractBizModel);
            long t2 = System.currentTimeMillis();
            logger.info("抽取{}章资产负债表，抽取资产负债表会计科目数据完成，耗时：{}毫秒。", getChapterModel().getChapterId(), (t2 - t1));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

    }

    /**
     * 资产负债
     */
    private void extractM2Xjll() {
        long t1 = System.currentTimeMillis();
        logger.info("开始抽取{}章现金流量表会计科目数据......", getChapterModel().getChapterId());
        ExtractBizModel extractBizModel = getChapterModel().getExtractBizModelMap().get("XJLL");

        //TODO 后续改成动态注入
        zcfzTokenizer.loadCoreSynonyms(extractBizModel);
        List<SectionModel> sectionModelList = getChapterModel().getSectionModelList();
        if (CollectionUtils.isEmpty(sectionModelList)) {
            logger.error("抽取{}章财务报表会计科目分析，会计科目分析列表为空，请确认！！！", getChapterModel().getChapterId());
            return;
        }
        try {
            for (int i = 0; i < sectionModelList.size(); i++) {
                SectionModel sectionModel = sectionModelList.get(i);
                List<SectionModel> childSectionModelList = sectionModel.getChildSectionModelList();
                if (CollectionUtils.isEmpty(childSectionModelList)) {
                    continue;
                }
                for (int j = 0; j < childSectionModelList.size(); j++) {
                    SectionModel childSectionModel = childSectionModelList.get(j);
                    String content = childSectionModel.getContent();
                    if (isEmpty(content)) {
                        continue;
                    }
                    logger.info("抽取{}章业务模型数据，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), extractBizModel.getModelName(), childSectionModel.getTitle());
                    zcfzTokenizer.splitWords5(extractBizModel, childSectionModel);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        zcfzTokenizer.unloadCoreSynonyms(extractBizModel);
        long t2 = System.currentTimeMillis();
        logger.info("抽取{}章现金流量表会计科目数据完成，耗时：{}毫秒。", getChapterModel().getChapterId(), (t2 - t1));
    }

    /**
     * 资产负债
     */
    private void extractM2Lr() {
        long t1 = System.currentTimeMillis();
        logger.info("开始抽取{}章利润表会计科目数据......", getChapterModel().getChapterId());
        ExtractBizModel extractBizModel = getChapterModel().getExtractBizModelMap().get("LR");

        //TODO 后续改成动态注入
        zcfzTokenizer.loadCoreSynonyms(extractBizModel);

        List<SectionModel> sectionModelList = getChapterModel().getSectionModelList();
        if (CollectionUtils.isEmpty(sectionModelList)) {
            logger.error("抽取{}章财务报表会计科目分析，会计科目分析列表为空，请确认！！！", getChapterModel().getChapterId());
            return;
        }
        try {
            for (int i = 0; i < sectionModelList.size(); i++) {
                SectionModel sectionModel = sectionModelList.get(i);
                List<SectionModel> childSectionModelList = sectionModel.getChildSectionModelList();
                if (CollectionUtils.isEmpty(childSectionModelList)) {
                    continue;
                }
                for (int j = 0; j < childSectionModelList.size(); j++) {
                    SectionModel childSectionModel = childSectionModelList.get(j);
                    String content = childSectionModel.getContent();
                    if (isEmpty(content)) {
                        continue;
                    }
                    logger.info("抽取{}章业务模型数据，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), extractBizModel.getModelName(), childSectionModel.getTitle());
                    zcfzTokenizer.splitWords5(extractBizModel, childSectionModel);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        zcfzTokenizer.unloadCoreSynonyms(extractBizModel);
        long t2 = System.currentTimeMillis();
        logger.info("抽取{}章利润表会计科目数据完成，耗时：{}毫秒。", getChapterModel().getChapterId(), (t2 - t1));
    }
}